<?php
include APPLICATION_PATH . "/models/Report5.php";
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Admin_Report5Controller extends Zend_Controller_Action{
    public function init(){       
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;
        //$this->modelMapper= new Model_ProvinceMapper();
        $this->model= new Model_Report5();
    }
    public function indexAction(){
       $this->view->controller = $this;  
       $this->view->agent_id = "";
       $now = date("Y/m/d H:i:s");
       $ar_now =    split("/",$now);
       if($this->getRequest()->isPost()){
            $this->view->nam = $_POST["nam"]; 
            
            if (isset($_POST['agent_id'])){
                $agent_id = array();
                foreach ($_POST['agent_id'] as $key => $value) {
                    array_push($agent_id,$value);                
                }            
                $this->view->agent_id = implode(",", $agent_id);
            }
       }
       else $this->view->nam = intval($ar_now[0]);       
       $html_nam = "<select id='nam' name='nam' class='form-control'> ";
       for($i = intval($this->view->nam)-5;$i<=intval($this->view->nam)+5;$i++)
       {
           if($i == $this->view->nam)
            $html_nam.="<option value='".$i."' selected='selected'>".$i."</option>";
           else
               $html_nam.="<option value='".$i."'>".$i."</option>";
       };
       $html_nam.="</select>";
       
       $this->view->html_nam = $html_nam;
       $this->view->item=$this->model->fetchAllReport($this->view->nam, $this->view->agent_id);
       $this->view->agentHTML=  GlobalLib::getComboMulti("agent_id", "agent", "id", "name","address",$this->view->agent_id,true,"where id IN (SELECT DISTINCT agent.id
                                                                                                                                                    FROM  `order` 
                                                                                                                                                    INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                                                                                                                                                    INNER JOIN  `agent` ON order.store_id = agent.id
                                                                                                                                                    WHERE order_detail.order_status_id = 2 AND YEAR( order_detail.order_created ) =".$this->view->nam.")");
        
        //Zend_Debug::dump($this->model->fetchAllReport());
    }
    public function serviceAction(){
        $this->_helper->layout->disableLayout();
        $nam = $this->_getParam("nam",2015);
        $agent_id = $this->_getParam("agent_id","");
        $menber   = array();
        foreach ($this->model->fetchAllReport($nam, $agent_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
            //$entries[] = $entry;
        }        
        echo json_encode($menber);        
        exit();
    }
    public function exportAction() {
        $this->_helper->layout->disableLayout();
        if ($this->getRequest()->isPost()) {
            $nam=$this->_getParam("nam","");
            $agent_id = $this->_getParam("agent_id","");
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);
            $rowCount = 4;
            $stt=1;
            $objPHPExcel->getActiveSheet()->setCellValue('A3', "STT");
            $objPHPExcel->getActiveSheet()->setCellValue('B3', "Tên Khách Hàng");
            $objPHPExcel->getActiveSheet()->setCellValue('C3', "Quý 1");
            $objPHPExcel->getActiveSheet()->setCellValue('D3', "Quý 2");
            $objPHPExcel->getActiveSheet()->setCellValue('E3', "Quý 3");
            $objPHPExcel->getActiveSheet()->setCellValue('F3', "Quý 4");
            $objPHPExcel->getActiveSheet()->setCellValue('G3', "Năm");
            $objPHPExcel->getActiveSheet()->setCellValue('H3', "Điểm");
            foreach ($this->model->fetchAllReportExport($nam, $agent_id) as $items) {
               
                $objPHPExcel->getActiveSheet()->setCellValue('A' . $rowCount, $stt);
                $objPHPExcel->getActiveSheet()->setCellValue('B' . $rowCount, $items->name_customer);
                $objPHPExcel->getActiveSheet()->setCellValue('C' . $rowCount, $items->quarter1);
                $objPHPExcel->getActiveSheet()->setCellValue('D' . $rowCount, $items->quarter2);
                $objPHPExcel->getActiveSheet()->setCellValue('E' . $rowCount, $items->quarter3);
                $objPHPExcel->getActiveSheet()->setCellValue('F' . $rowCount, $items->quarter4);
                $objPHPExcel->getActiveSheet()->setCellValue('G' . $rowCount, $items->total_quantity);
                $objPHPExcel->getActiveSheet()->setCellValue('H' . $rowCount, $items->charges);
                $rowCount++;
                $stt++;
            }
            $objPHPExcel->getActiveSheet()->mergeCells('A1:H2');
//            $objDrawing = new PHPExcel_Worksheet_Drawing();
//            $objDrawing->setPath('../../image/images answer.png');
//            $objDrawing->setCoordinates('A1');
//            $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
//            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(120);
            $objPHPExcel->getActiveSheet()->setCellValue('A1', "Báo Cáo Doanh Số Theo Quý Của Năm ".$nam);
            $style_alignment = array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A3:H" . $rowCount)->getAlignment()->applyFromArray($style_alignment);
            $styleArray = array(
                'borders' => array(
                    'allborders' => array(
                        'style' => PHPExcel_Style_Border::BORDER_THIN,
                    ),
                ),
            );
            $styledataArray = array(
                'font' => array(
                    'bold' => true,
                    'color' => array('rgb' => '080808'),
                    'size' => 15,
                    'name' => 'Times New Roman'
            ));
            $styletitleArray=array(
                'font' => array(
                    'bold' => true,
                    'color' => array('rgb' => '080808'),
                    'size' => 10,
                    'name' => 'Times New Roman')
            );
            $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->applyFromArray($style_alignment);
            $objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styledataArray);
            $objPHPExcel->getActiveSheet()->getStyle('A3:H3')->applyFromArray($styletitleArray);
            $objPHPExcel->getActiveSheet()->getStyle('A3:H' . ($rowCount - 1))->applyFromArray($styleArray);
            foreach (range('A', 'H') as $columnID) {
                $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
            }
            $filename='BaoCaoDoanhSoTheoQuýCuaNam '.$nam.'.xls';
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename='.$filename);
            header('Cache-Control: max-age=0');
            $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
            $objWriter->save('php://output');
            exit();
        }
    }
}
